package com.datasecops.scannerservices.extractwatermark;

import com.datasecops.scannerservices.configurationfiles.WatermarkingConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.*;
import java.sql.SQLException;

/**提取水印的配置文件
 * @author: GeXiaoLong
 * @time: 2023/1/12 10:42
 */
@Service
@Slf4j
public class ExtractionConfiguration {
    //获取生成提取水印配置文件
    public static void diskDropFile(WatermarkingConfiguration ss) throws IOException {
        File outFile = new File("/root/watermark/dso_dbwm/WmCfg.txt");
        //File outFile = new File("P:\\text\\WmCfg.txt");
        boolean exists = outFile.exists();
        if (exists){
            outFile.delete();
        }else {
            outFile.createNewFile();
        }

        Writer out ;
        String host = ss.getHost();
        String port = ss.getPort();
        String user = ss.getUser();
        String password = ss.getPassword();
        String dbName = ss.getDbName();
        String extractTableName = ss.getExtractTableName();
        String extractWmFileNameName = ss.getExtractWmFileNameName();
        String extractAuxInfoFileName = ss.getExtractAuxInfoFileName();


        String embedWmFileName = ss.getEmbedWmFileName();
        String embedAuxInfoFileName = ss.getEmbedAuxInfoFileName();

        String runOutFileName = ss.getRunOutFileName();
        String tableDesFileName = ss.getTableDesFileName();
        String wmType = ss.getWmType();
        String operType = ss.getOperType();
        String traceWmEmbedChars = ss.getTraceWmEmbedChars();
        String characterSet = ss.getCharacterSet();
        String dbLoaded = ss.getDbLoaded();
        String reUseTableInfo = ss.getReUseTableInfo();
        String splitChars = ss.getSplitChars();
        String splitLineChars = ss.getSplitLineChars();
        String enclosedChars = ss.getEnclosedChars();
        String mysqlClientSo = ss.getMysqlClientSo();
        String fakeRowEmbedProp = ss.getFakeRowEmbedProp();
        String distortThreshold = ss.getDistortThreshold();
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile,true),"utf-8"),10240);
            InFile(out, host, port, user, password, dbName, extractTableName, extractWmFileNameName, extractAuxInfoFileName,
                    embedWmFileName,embedAuxInfoFileName,runOutFileName, tableDesFileName);
            InFile(out, wmType,operType, traceWmEmbedChars, characterSet, dbLoaded, reUseTableInfo, splitChars, splitLineChars, enclosedChars, mysqlClientSo, fakeRowEmbedProp, distortThreshold);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void InFile(Writer out, String host, String port, String user, String password, String dbName, String extractTableName,
                               String extractWmFileNameName, String extractAuxInfoFileName,
                               String embedWmFileName,String embedAuxInfoFileName,
                               String runOutFileName, String tableDesFileName) throws IOException {
        Infiles(out, host, port, user, password, dbName,extractTableName);
        Infiles(out, extractWmFileNameName, extractAuxInfoFileName,embedWmFileName,embedAuxInfoFileName,runOutFileName, tableDesFileName);
        //out.write(wmType);
        out.write("\n");
    }

    public static void Infiles(Writer out, String host, String port, String user, String password, String dbName,String extractTableName) throws IOException {
        out.write(host);
        out.write("\n");
        out.write(port);
        out.write("\n");
        out.write(user);
        out.write("\n");
        out.write(password);
        out.write("\n");
        out.write(dbName);
        out.write("\n");
        out.write(extractTableName);
        out.write("\n");
    }

    public static void main(String[] args) throws SQLException, IOException {
        WatermarkingConfiguration ss = new WatermarkingConfiguration();
        String DbName = "soul";
        String OperType = "OtEmbed";
        String EmbedRableNAme = "student";
        String WmType = "WmFakeRow,WmDigitalAsb,WmDigitalHsw";
        String TraceWmEmbedChars = "DSO Database IPR, 2022.11.11, China Beijing. To:China Mobile. Usage:Collaboration, Validity:2200.12.31.";
        String dbName = ss.getDbName()+DbName;
        ss.setDbName(dbName);
        String operType = ss.getOperType()+OperType;
        ss.setOperType(operType);
        if ("OtEmbed".equalsIgnoreCase(OperType)){
            String embedTableName = ss.getEmbedTableName()+EmbedRableNAme;
            ss.setEmbedTableName(embedTableName);
            String extractWmFileNameName = ss.getEmbedWmFileName()+EmbedRableNAme+".txt";
            ss.setEmbedWmFileName(extractWmFileNameName);
            String extractAuxInfoFileName = ss.getEmbedAuxInfoFileName()+EmbedRableNAme+".txt";
            ss.setEmbedAuxInfoFileName(extractAuxInfoFileName);
        }
        String runOutFileName = ss.getRunOutFileName()+EmbedRableNAme+".log";
        ss.setRunOutFileName(runOutFileName);
        String tableDesFileName = ss.getTableDesFileName()+EmbedRableNAme+".txt";
        ss.setTableDesFileName(tableDesFileName);
        String wmType = ss.getWmType()+WmType;
        ss.setWmType(wmType);
        String traceWmEmbedChars = ss.getTraceWmEmbedChars()+TraceWmEmbedChars;
        ss.setTraceWmEmbedChars(traceWmEmbedChars);
        diskDropFile(ss);

    }

}
